/*
 * hfattach  附件操作工具
 * 注意事项:
 * auther : "HaoRong",
 * Date: 2018-05-22
 * Version : 1.0
 */

    /**是否编辑枚举*/
    var IS_EDIT={
        YES:1,
        NO:0
    }
    /**附件功能操作显示*/
    function attachOperate (value, row, index) {
        var lOption=[
            '<a class="query" href="javascript:attach_download(\''+ row.attach_id + '\');" title="download"><i class="glyphicon glyphicon-download"></i></a>&nbsp;'
        ];
        lOption.push('<a class="query" href="javascript:attach_info_del(\''+ row.id + '\');" title="remove"><i class="glyphicon glyphicon-remove"></i>,</a>&nbsp;');
        return lOption.join('');;
    };
    /**字节转兆*/
    function byteToMb(value, row, index){
        var unit = "";
        var size;
        var byte = row.attach_size;
        if((byte/1024/1024) > 1){
            size = byte/1024/1024;
            unit = "M";
            size = size.toFixed(2);
        }else{
            size = byte/1024;
            size = size.toFixed(2);
            unit = "kb";
        }
        return [ size+unit ]
            .join('');
    }


    /**下载附件*/
    function attach_download(id){
        window.location.href="/hfattach/service/download/Download.do?id="+id;
    };

    /**获取附件列表*/
    function getAttachList(data, callbackfunc){
        HFAjax("/hfattach/service/attach/getAttachList.do", data, function(result){
            callbackfunc(result);
        });
    };

    /**获取附件列表(根据attach_code)*/
    function getAttachByCode(data, callbackfunc){
        HFAjax("/hfattach/service/attach/getAttachByCode.do", data, function(result){
            callbackfunc(result);
        });
    };

    /**获取附件列表(根据info_id)*/
    function getAttachByInfoId(data, callbackfunc){
        HFAjax("/hfattach/service/attach/getAttachByInfoId.do", data, function(result){
            callbackfunc(result);
        });
    };

    /**获取附件列表（根据codes）*/
    function getAttachByCodes(data, callbackfunc){
        HFAjax("/hfattach/service/attach/getAttachByCodes.do", data, function(result){
            callbackfunc(result);
        });
    };

    /**获取附件列表（根据ids）*/
    function getAttachByInfoIds(data, callbackfunc){
        HFAjax("/hfattach/service/attach/getAttachByInfoIds.do", data, function(result){
            callbackfunc(result);
        });
    };

    /**删除附件*/
    function attach_file_del(attach_code){
        if(attach_code != undefined && attach_code != ""){
            HFConfirm("确认要删除选中该信息吗？", "", "warning", function(isConfirm){
                if(isConfirm){
                    HFAjax("/hfattach/service/attach/attachfiledel.do", {attach_code: attach_code}, function(result){
                        HFToastrTipResult(result);
                        refreshBSTable('attach-table');
                    });
                }
            });
        }else{
            HFToastrError("请选择要删除的信息！");
        }
    }

    /**删除附件信息*/
    function attach_info_del(attach_info_id){
        if(attach_info_id != undefined && attach_info_id != ""){
            HFConfirm("确认要删除选中该信息吗？", "", "warning", function(isConfirm){
                if(isConfirm){
                    HFAjax("/hfattach/service/attach/attachinfodel.do", {attach_info_id: attach_info_id}, function(result){
                        HFToastrTipResult(result);
                        refreshBSTable('attach-table');
                    });
                }
            });
        }else{
            HFToastrError("请选择要删除的信息！");
        }
    }

function HFAttach() {
    /**初始化附件*/
    this.init = function (attachIds,attachListID,sourceId,isEdit) {
        //初始化附件列表
        this.initAttachList(attachIds,attachListID,sourceId,isEdit);
        if(isEdit==IS_EDIT.YES){
            //初始化上传事件
            this.initAttachEvent();
        }
    };
    /**初始化附件列表*/
    this.initAttachList=function(attachIds,attachListID,sourceId,isEdit){
        //初始化相关附件
        if(attachIds.length>0) {
            getAttachByInfoIds({attach_info_ids:attachIds},function(data){
                // alert(JSON.stringify(data));
                if(data.ok==true) {
                    //console.log(data);
                    var result = data.result;
                    $.each( result, function(i, n){
                        var info = n;
                        var strhtml = $('#'+attachListID).html();
                        strhtml += '<tr class="attach" data_code="'+info.id+'">';
                        strhtml += '<td>'+info.attach_name+'</td>';
                        strhtml += '<td>'+byteToMb(0,info,0)+'</td>';
                        strhtml += '<td>'+info.attach_user+'</td>';
                        strhtml += '<td>'+info.attach_time+'</td>';
                        strhtml += '<td align="center">';
                        strhtml += '&nbsp;<a href="javascript:javascript:void(0);" onclick="attach_download(\''+info.id+'\');return false;">下载</a>&nbsp;';
                        if (isEdit==IS_EDIT.YES){
                            strhtml +='&nbsp;<a href="javascript:javascript:void(0);" onclick="delItem(this,\''+sourceId+'\',\''+info.id+'\',\'attach_ids\');return false;">删除</a>&nbsp;';
                        }
                        strhtml +='</td>';
                        strhtml +='</tr>';
                        $('#'+attachListID).html(strhtml);
                    });
                }
            });
        }
    };
    /**初始化上传事件*/
    this.initAttachEvent=function(){
        $('.yxAjaxUpload').yxAjaxUpload({
            success : uploadCallBack,
            complete : function(res){
                if(jQuery.isEmptyObject(res)){
                    HFToastrError("添加失败，请检查您的文件是否符合规范和文件大小是否超过${maxSize}M！");
                    $('#submitBtn').removeAttr("disabled");
                }
            },
            dataType : 'text',
            url : "/hfattach/service/upload/uploadfile.do",
            submitBtn : "submitBtn"
        });
    };



}